
class Bag
  def initialize
    @content = {}
  end
  def has?(obj)
    @content.has_key?(obj)
  end
  def add(obj)
    if has?(obj)
      @content[obj] += 1
    else
      @content[obj] = 1
    end
  end

  def remove(obj)
    @content[obj] -= 1 if has?(obj)
    @content.delete(obj) if @content[obj] == 0
  end

  def sorted_counts
    arr = []
    @content.keys.each do |key|
      arr << [@content[key], key]
    end
    arr.sort{|a,b| a.first <=> b.first}.reverse
  end
  def size
    @content.keys.inject(0) { |total, key| total + @content[key] }
  end
end
